<template>
    <div>
        <!-- 分类 -->
        <div id="slider" class="mui-slider">
            <div id="sliderSegmentedControl" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
                <div class="mui-scroll">
                    <a :class="['mui-control-item', item.id == 0 ? 'mui-active' : '']" v-for="item in cates" :key="item.id" @tap="getPhotoListByCateId(item.id)">
                        {{item.title}}
                    </a>
                </div>
            </div>
        </div>

        <!-- 列表 -->
        <ul class="photo-list">
            <router-link v-for="item in list" :key="item.id" :to="'/home/photoinfo/' + item.id" tag="li">
                <img v-lazy="item.img_url">
                <div class="list-info">
                    <h3 class="info-title">{{item.title}}</h3>
                    <div class="info-body">{{item.zhaiyao}}</div>
                </div>
            </router-link>
        </ul>
    </div>
</template>

<script>

// 引入 mui 的 js文件
import mui from '../../lib/mui/js/mui.min.js';
import { Toast } from 'mint-ui';

export default {
    data() {
        return {
            cates: [], // 分类数据
            list: [] // 列表数据       
        }
    },

    created() {
        this.getCategory(),
        this.getPhotoListByCateId(0) // 默认初始加载全部
    },

    mounted () {
        // 在页面挂在完成后 初始化scroll控件：
        // mui('.mui-scroll-wrapper').scroll({
        //     scrollY: false, //是否竖向滚动
        //     scrollX: true, //是否横向滚动
        //     startX: 0, //初始化时滚动至x
        //     startY: 0, //初始化时滚动至y
        //     indicators: true, //是否显示滚动条
        //     deceleration:0.0006, //阻尼系数,系数越小滑动越灵敏
        //     bounce: true, //是否启用回弹
        //     deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
        // })
        // console.log('ok');
        mui('.mui-scroll-wrapper').scroll().setTranslate(0,0)
    
    },

    methods: {
        // 获取分类
        getCategory(){
            this.$http.get('api/getimgcategory').then(res=>{
                
                if (res.body.status === 0) {
                    res.body.message.unshift({title: '全部', id:0 });
                    this.cates = res.body.message;
                }
            })
        },

        // 获取图片列表
        getPhotoListByCateId(cateId){
            this.$http.get('api/getimages/'+ cateId).then(res=>{
                if (res.body.status === 0) {
                    this.list = res.body.message
                } else {
                    Toast('图片列表加载失败');
                }
            })
        }
    }

}
</script>

<style lang="less" scoped>
    * {
        touch-action: pan-y;
    }

    .photo-list {
        margin: 0;
        padding: 10px;
        list-style: none;
        padding-bottom: 0;
        li {
            position: relative;
            text-align: center;
            background-color: #ccc;
            margin-bottom: 10px;
            box-shadow: 0 0 6px rgba(0,0,0,.4);
            img {
                width: 100%;
                vertical-align: middle;
            }
            img[lazy=loading] {
                width: 40px;
                height: 300px;
                margin: auto;
            }
            .list-info {
                position: absolute;
                left: 0;
                bottom: 0;
                max-height: 84px;
                color: #fff;
                padding: 0 5px;
                text-align: left;
                background: rgba(0,0,0,.4);
                h3 {
                    font-size: 14px;
                }

                .info-body {
                    font-size: 12px;
                }
            }
        }
    }
</style>

